let app = getApp();
const api = require('../../server/Api.js');
const _user = require("../../utils/User.js")
Page({
  data: {
    //判断小程序的API，回调，参数，组件等是否在当前版本可用。
    // canIUse: wx.canIUse('button.open-type.getUserInfo'),
    userInfo: {}
  },

  onLoad: function (options) {

  },

  onShow: function () {

  },
  //获取用户信息
  bindGetUserInfo: function (e) {
    let that = this;
    app.showLoading()
    app.wxLogin().then(res => {
      tt.hideLoading()
      let userBean = res;
      if (userBean) {
        app.globalData.userBean = res;
        _user.saveUser(userBean);
        app.backHome();
      }
    }).catch(msg => {
      tt.hideLoading()
      that.wxRegister(e);
    });
  },
  //头条登录
  ttLogin: function () {
    let that = this;
    app.showLoading()
    app.ttLogin().then(res => {
      tt.hideLoading()
      let userBean = res;
      if (userBean) {
        app.globalData.userBean = res;
        _user.saveUser(userBean);
        app.backHome();
      }
    }).catch(msg => {
      tt.hideLoading()
      tt.getUserInfo({
        success(res) {
          console.log(res.userInfo);
          app.globalData.userInfo = res.userInfo;
          that.wxRegister();
        }
      })

    });
  },
  //微信注册
  wxRegister: function (e) {
    let that = this;
    let userInfo = app.globalData.userInfo;
    if (userInfo) {
      let ttName = userInfo.nickName;
      let avatarUrl = userInfo.avatarUrl;
      let country = userInfo.country;
      let province = userInfo.province;
      let city = userInfo.city;

      // app.globalData.userInfo = userInfo;
      tt.navigateTo({
        url: '../login-referral-code/login-referral-code',
      })
    }

  },

  //手机登录
  onTapMobile: function (e) {
    // let encryptedData = e.detail.encryptedData;
    // let iv = e.detail.iv;
    // if (!encryptedData || !iv){
    //   return;
    // }
    tt.navigateTo({
      url: '../login-mobile-sim/login-mobile-sim',
    })
  },


  onShareAppMessage: function () {

  }
})